package br.edu.fatecou.reserva.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import br.edu.fatecou.reserva.dao.CategoriaDAO;
import br.edu.fatecou.reserva.dominio.Categoria;

@FacesConverter(value="categoriaConvert", forClass=Categoria.class)
public class CategoriaConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
		System.out.println(value);
		if(value != null && !"".equals(value)) {
			try {
				System.out.println("entrou no if");
				Categoria categoria = new CategoriaDAO().buscar(Long.parseLong(value));
				System.out.println(categoria.getNome());
				return categoria;
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
		if(object != null && object instanceof Categoria) {
			return ((Categoria)object).getId().toString();
		}
		return null;
	}
}